﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace YJ.Data.MSSQL
{
    public class ProgramBuilderButtons : YJ.Data.Interface.IProgramBuilderButtons
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgramBuilderButtons()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderButtons实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.ProgramBuilderButtons model)
        {
            string sql = @"INSERT INTO ProgramBuilderButtons
				(ID,ProgramID,ButtonID,ButtonName,ClientScript,Ico,ShowType,Sort,IsValidateShow) 
				VALUES(@ID,@ProgramID,@ButtonID,@ButtonName,@ClientScript,@Ico,@ShowType,@Sort,@IsValidateShow)";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID },
				new SqlParameter("@ProgramID", SqlDbType.UniqueIdentifier, -1){ Value = model.ProgramID },
				model.ButtonID == null ? new SqlParameter("@ButtonID", SqlDbType.UniqueIdentifier, -1) { Value = DBNull.Value } : new SqlParameter("@ButtonID", SqlDbType.UniqueIdentifier, -1) { Value = model.ButtonID },
				new SqlParameter("@ButtonName", SqlDbType.NVarChar, 400){ Value = model.ButtonName },
				model.ClientScript == null ? new SqlParameter("@ClientScript", SqlDbType.VarChar, -1) { Value = DBNull.Value } : new SqlParameter("@ClientScript", SqlDbType.VarChar, -1) { Value = model.ClientScript },
				model.Ico == null ? new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = model.Ico },
				new SqlParameter("@ShowType", SqlDbType.Int, -1){ Value = model.ShowType },
				new SqlParameter("@Sort", SqlDbType.Int, -1){ Value = model.Sort },
				new SqlParameter("@IsValidateShow", SqlDbType.Int, -1){ Value = model.IsValidateShow }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderButtons实体类</param>
        public int Update(YJ.Data.Model.ProgramBuilderButtons model)
        {
            string sql = @"UPDATE ProgramBuilderButtons SET 
				ProgramID=@ProgramID,ButtonID=@ButtonID,ButtonName=@ButtonName,ClientScript=@ClientScript,Ico=@Ico,ShowType=@ShowType,Sort=@Sort,IsValidateShow=@IsValidateShow
				WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ProgramID", SqlDbType.UniqueIdentifier, -1){ Value = model.ProgramID },
				model.ButtonID == null ? new SqlParameter("@ButtonID", SqlDbType.UniqueIdentifier, -1) { Value = DBNull.Value } : new SqlParameter("@ButtonID", SqlDbType.UniqueIdentifier, -1) { Value = model.ButtonID },
				new SqlParameter("@ButtonName", SqlDbType.NVarChar, 400){ Value = model.ButtonName },
				model.ClientScript == null ? new SqlParameter("@ClientScript", SqlDbType.VarChar, -1) { Value = DBNull.Value } : new SqlParameter("@ClientScript", SqlDbType.VarChar, -1) { Value = model.ClientScript },
				model.Ico == null ? new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = model.Ico },
				new SqlParameter("@ShowType", SqlDbType.Int, -1){ Value = model.ShowType },
				new SqlParameter("@Sort", SqlDbType.Int, -1){ Value = model.Sort },
				new SqlParameter("@IsValidateShow", SqlDbType.Int, -1){ Value = model.IsValidateShow },
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM ProgramBuilderButtons WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.ProgramBuilderButtons> DataReaderToList(SqlDataReader dataReader)
        {
            List<YJ.Data.Model.ProgramBuilderButtons> List = new List<YJ.Data.Model.ProgramBuilderButtons>();
            YJ.Data.Model.ProgramBuilderButtons model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.ProgramBuilderButtons();
                model.ID = dataReader.GetGuid(0);
                model.ProgramID = dataReader.GetGuid(1);
                if (!dataReader.IsDBNull(2))
                    model.ButtonID = dataReader.GetGuid(2);
                model.ButtonName = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.ClientScript = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Ico = dataReader.GetString(5);
                model.ShowType = dataReader.GetInt32(6);
                model.Sort = dataReader.GetInt32(7);
                model.IsValidateShow = dataReader.GetInt32(8);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderButtons> GetAll()
        {
            string sql = "SELECT * FROM ProgramBuilderButtons";
            SqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.ProgramBuilderButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM ProgramBuilderButtons";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.ProgramBuilderButtons Get(Guid id)
        {
            string sql = "SELECT * FROM ProgramBuilderButtons WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderButtons> GetAll(Guid id)
        {
            string sql = "SELECT * FROM ProgramBuilderButtons WHERE ProgramID=@ProgramID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ProgramID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}